//
//  xsBackgroundView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/29.
//  选择背景
// @[@"我的",@"纯色",@"官方"]

#import "xsBackgroundView.h"
#import "xsBackgroundViewTopTitleModel.h"
#import "xsAuthorityBackGroundStyleCollectionView.h"
  
#define xrandomColor  [UIColor colorWithRed:(arc4random() %255)/255.0f green:(arc4random() %255)/255.0f blue:(arc4random() %255)/255.0f alpha:1.0f]

@interface xsBackgroundView()<UIScrollViewDelegate,xsAuthorityBackGroundStyleCollectionViewProtoCol>

@property (nonatomic, strong) UIView *xsAuthorityBackGroundStyleBgView;
@property (nonatomic, strong) UIView *xsAuthorityBackGroundStyleCenterColloectionBgView;
@property (nonatomic, strong) UILabel *xsAuthorityBackGroundStyleTopTitleLabel;
@property (nonatomic, strong) UIButton *xsAuthorityBackGroundStyleBottomCheckButton;// 确认选择
@property (nonatomic, strong) UIButton *xsAuthorityBackGroundStyleTopRightCancelButton;// 取消选择
@property (nonatomic,strong)xsAuthorityBackGroundStyleCollectionView *xsAuthorityBackGroundStyleCollectionV;
@property (nonatomic,strong)PhotoCellModel *recodeSelectPhotoCellMd;

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UILabel *topTitleLabel;
@property (nonatomic, strong) UIButton *xsXbutton;// ✅按钮


@property (nonatomic, strong) UIScrollView *buttonsBgScrollView;
//@property (nonatomic, strong) NSArray *buttons;
@property (nonatomic, strong) UIView *buttonsBgView;

@property (nonatomic, strong) UIScrollView *CollectionBgScrollView;

@property (nonatomic,strong)xsBossMyCollectionView *BossMyCollectionView;//我的
@property (nonatomic,strong)xsSolidColorCollectionView *SolidColorCollectionView;//纯色
@property (nonatomic,strong)xsAuthorityCollectionView *AuthorityCollectionView;//官方

@property (nonatomic, strong) NSMutableArray *topButtonTitleMarray;

@end

@implementation xsBackgroundView

- (UIView *)xsAuthorityBackGroundStyleBgView{
    
    if(!_xsAuthorityBackGroundStyleBgView){
        _xsAuthorityBackGroundStyleBgView = [[UIView alloc] init];
        _xsAuthorityBackGroundStyleBgView.backgroundColor = COLOR_Black333;
    }
    return _xsAuthorityBackGroundStyleBgView;
}


//- (xsAuthorityBackGroundStyleCollectionView *)xsAuthorityBackGroundStyleCollectionV{
//    
//    if(!_xsAuthorityBackGroundStyleCollectionV){
//        
//        // 使用系统自带的流布局（继承自UICollectionViewLayout）
//        TWLayout *layout = ({
//            TWLayout *layout = [[TWLayout alloc] init];
//            layout.itemSize                    = CGSizeMake(180, 180);
//            layout.scrollDirection             = UICollectionViewScrollDirectionHorizontal;
//            layout.minimumLineSpacing          = 10;//40;
//            
//            
//            //第一个cell和最后一个cell居中显示
//            CGFloat margin = (self.width - 160) * 0.5;
//            layout.sectionInset                = UIEdgeInsetsMake(0, margin, 0, margin);
//            
//            layout;
//        });
//        
//        // 使用UICollectionView必须设置UICollectionViewLayout属性
//        UICollectionView *collectionView = ({
//            UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
//            collectionView.center            = self.center;
//            collectionView.bounds            = CGRectMake(0, 0, self.width, 200);
//            collectionView.backgroundColor   = [UIColor brownColor];
//            collectionView.dataSource        = self;
//            [collectionView setShowsHorizontalScrollIndicator:NO];
//            
//            [self addSubview:collectionView];
//            
//            collectionView;
//        });
//        
//    }
//    return _AuthorityCollectionView;
//}


- (UIView *)xsAuthorityBackGroundStyleCenterColloectionBgView{
    
    if(!_xsAuthorityBackGroundStyleCenterColloectionBgView){
        _xsAuthorityBackGroundStyleCenterColloectionBgView = [[UIView alloc] init];
    }
    return _xsAuthorityBackGroundStyleCenterColloectionBgView;
}

- (UILabel *)xsAuthorityBackGroundStyleTopTitleLabel {
    if (!_xsAuthorityBackGroundStyleTopTitleLabel) {
        _xsAuthorityBackGroundStyleTopTitleLabel = [[UILabel alloc] init];
        _xsAuthorityBackGroundStyleTopTitleLabel.backgroundColor = UIColor.clearColor;
        _xsAuthorityBackGroundStyleTopTitleLabel.font = [UIFont systemFontOfSize:21];// FontSemibold(17);
        _xsAuthorityBackGroundStyleTopTitleLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _xsAuthorityBackGroundStyleTopTitleLabel.numberOfLines = 1;
        _xsAuthorityBackGroundStyleTopTitleLabel.text = @"选择背景图片样式";
        _xsAuthorityBackGroundStyleTopTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _xsAuthorityBackGroundStyleTopTitleLabel;
}
- (UIButton *)xsAuthorityBackGroundStyleBottomCheckButton{
    if (!_xsAuthorityBackGroundStyleBottomCheckButton) {
        _xsAuthorityBackGroundStyleBottomCheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsAuthorityBackGroundStyleBottomCheckButton.backgroundColor = COLOR_Blue;
        [_xsAuthorityBackGroundStyleBottomCheckButton addTarget:self action:@selector(xsAuthorityBackGroundStyleBottomCheckButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [_xsAuthorityBackGroundStyleBottomCheckButton setImage:[UIImage imageNamed:@"xsBossIPX"] forState:UIControlStateNormal];
        [_xsAuthorityBackGroundStyleBottomCheckButton setTitle:kLocalizedString(@"确认选择背景图") forState:UIControlStateNormal];
        [_xsAuthorityBackGroundStyleBottomCheckButton setTitleColor:COLOR_textWhiteColor forState:UIControlStateNormal];
        _xsAuthorityBackGroundStyleBottomCheckButton.font = Font(16);
    }
    return _xsAuthorityBackGroundStyleBottomCheckButton;
}

#pragma mark 确认选择背景图
- (void)xsAuthorityBackGroundStyleBottomCheckButtonAction:(UIButton *)btn{
    NSLog(@"xsAuthorityBackGroundStyleBottomCheckButtonAction\n\n");

    
    if(!self.recodeSelectPhotoCellMd){
        [MBProgressHUD showError:kLocalizedString(@"请先选择背景图!")];
        return;
    }
    //self.recodeSelectPhotoCellMd = cellModel;
    xsAuthorityCollectionViewCellModel *AuthorityCollectionViewCellModel = [xsAuthorityCollectionViewCellModel new];
    AuthorityCollectionViewCellModel.url = self.recodeSelectPhotoCellMd.url;
//    // 记录选中的图片地址
//    @property (nonatomic,copy) NSString *xsRecordSelectImageUrl;
//    // 记录选中的图片地址对应的图片类型(全屏虚化或者四周虚化或者动图...)
//    @property (nonatomic,copy) NSString *xsRecordSelectImageName;
    AuthorityCollectionViewCellModel.xsRecordSelectImageUrl = self.recodeSelectPhotoCellMd.url;
    AuthorityCollectionViewCellModel.xsRecordSelectImageName = self.recodeSelectPhotoCellMd.title;
    
        if([self.delegate respondsToSelector:@selector(xsBackgroundCollectionViewCellCallBackWithModel:withCollectionTyp:)]){
            [self.delegate xsBackgroundCollectionViewCellCallBackWithModel:AuthorityCollectionViewCellModel withCollectionTyp:2];
    }
    
    self.xsAuthorityBackGroundStyleBgView.hidden = YES;
    
    

    NSArray *AuthorityCollectionVArr = [self.AuthorityCollectionView xsCollectionViewDataArray];
    for (xsAuthorityCollectionViewCellModel *temp3Md in AuthorityCollectionVArr) {
        if(temp3Md.Id!=self.recodeSelectPhotoCellMd.Id){
            
            temp3Md.isSelect = NO;
        
        }
        else{
            temp3Md.isSelect = self.recodeSelectPhotoCellMd.isSelect;
        }
    }
    [self.AuthorityCollectionView reloadData];
}

- (UIButton *)xsAuthorityBackGroundStyleTopRightCancelButton{
    if (!_xsAuthorityBackGroundStyleTopRightCancelButton) {
        _xsAuthorityBackGroundStyleTopRightCancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsAuthorityBackGroundStyleTopRightCancelButton.backgroundColor = UIColor.clearColor;
        [_xsAuthorityBackGroundStyleTopRightCancelButton addTarget:self action:@selector(xsAuthorityBackGroundStyleTopRightCancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsAuthorityBackGroundStyleTopRightCancelButton setImage:[UIImage imageNamed:@"xsBossgou"] forState:UIControlStateNormal];
    }
    return _xsAuthorityBackGroundStyleTopRightCancelButton;
}
#pragma mark 取消选择背景图
- (void)xsAuthorityBackGroundStyleTopRightCancelButtonAction:(UIButton *)btn{
    NSLog(@"xsAuthorityBackGroundStyleTopRightCancelButtonAction\n\n");
   
//    if([self.delegate respondsToSelector:@selector(xsBackgroundViewXbuttonBeClick:)]){
//        [self.delegate xsBackgroundViewXbuttonBeClick:btn];
//    }
    self.recodeSelectPhotoCellMd = nil;
    self.xsAuthorityBackGroundStyleBgView.hidden = YES;
}


- (NSMutableArray *)topButtonTitleMarray{
    if(!_topButtonTitleMarray){
        _topButtonTitleMarray = [[NSMutableArray alloc] init];
    }
    return _topButtonTitleMarray;
}

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles{
    
    if (self = [super init]) {
        self.backgroundColor = UIColor.blackColor;
        
        
        [self addSubview:self.topView];
        
        [self.topView addSubview:self.searchButton];
        [self.topView addSubview:self.topTitleLabel];
        [self.topView addSubview:self.xsXbutton];
        
        [self addSubview:self.buttonsBgScrollView];
            
        [self.buttonsBgScrollView addSubview:self.buttonsBgView];
        
        
//        [self addSubview:self.DigitalRobotCollectionView];
        [self addSubview:self.CollectionBgScrollView];
        
        [self.CollectionBgScrollView addSubview:self.BossMyCollectionView];
        [self.CollectionBgScrollView addSubview:self.SolidColorCollectionView];
        [self.CollectionBgScrollView addSubview:self.AuthorityCollectionView];
            
            // 创建按钮数组
//        self.buttons = buttonTitles;//@[@"Tab1", @"Tab2", @"Tab3"];
            
        [self addSubview:self.xsAuthorityBackGroundStyleBgView];
        [self.xsAuthorityBackGroundStyleBgView addSubview:self.xsAuthorityBackGroundStyleTopTitleLabel];
        [self.xsAuthorityBackGroundStyleBgView addSubview:self.xsAuthorityBackGroundStyleTopRightCancelButton];
        [self.xsAuthorityBackGroundStyleBgView addSubview:self.xsAuthorityBackGroundStyleCenterColloectionBgView];
        [self.xsAuthorityBackGroundStyleBgView addSubview:self.xsAuthorityBackGroundStyleBottomCheckButton];
        
        self.xsAuthorityBackGroundStyleBgView.hidden = YES;
        
    }
    return self;
}


- (UIView *)buttonsBgView{
    if(!_buttonsBgView){
        _buttonsBgView = [[UIView alloc] init];
        _buttonsBgView.backgroundColor = UIColor.blackColor;
    }
    return _buttonsBgView;
}


//@property (nonatomic,strong) UIScrollView *buttonsBgScrollView;
//[self.bgView addSubview:self.buttonsBgScrollView];
//[self.buttonsBgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//    make.edges.equalTo(self.bgView);
//}];
- (UIScrollView *)buttonsBgScrollView
{
    if(!_buttonsBgScrollView){
        _buttonsBgScrollView = [[UIScrollView alloc] init];
        _buttonsBgScrollView.backgroundColor = UIColor.clearColor;
        
        _buttonsBgScrollView.delegate = self;
//        _buttonsBgScrollView.pagingEnabled = YES;
        _buttonsBgScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _buttonsBgScrollView;
}


- (UIScrollView *)CollectionBgScrollView
{
    if(!_CollectionBgScrollView){
        _CollectionBgScrollView = [[UIScrollView alloc] init];
        _CollectionBgScrollView.backgroundColor = UIColor.clearColor;
        
        _CollectionBgScrollView.delegate = self;
        _CollectionBgScrollView.pagingEnabled = YES;
        //_CollectionBgScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _CollectionBgScrollView;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
    
}
- (void)xsSetFrame{
    CGFloat margin10 = 10;
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(margin10);
        make.top.equalTo(self.mas_top);//.offset(10);
        make.right.equalTo(self.mas_right).offset(-margin10);
        make.height.mas_equalTo(50);
    }];
    
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topView.mas_left);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.xsXbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.topView.mas_right);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topView);
    }];
    
    
    
    self.buttonsBgScrollView.frame = CGRectMake(0, 50+margin10, self.bounds.size.width, 40);
    
    
    
    CGFloat CollectionBgScrollViewX = margin10;
    CGFloat CollectionBgScrollViewY = CGRectGetMaxY(self.buttonsBgScrollView.frame)+margin10;
    CGFloat CollectionBgScrollViewW = self.width - CollectionBgScrollViewX*2.0;
    CGFloat CollectionBgScrollViewH = self.height - CollectionBgScrollViewY;
    
    self.CollectionBgScrollView.frame = CGRectMake(CollectionBgScrollViewX, CollectionBgScrollViewY, CollectionBgScrollViewW, CollectionBgScrollViewH);
    self.CollectionBgScrollView.backgroundColor = UIColor.clearColor;
    self.CollectionBgScrollView.contentSize = CGSizeMake(CollectionBgScrollViewW*3.0, 0);
    
    
    
    
    self.BossMyCollectionView.frame = CGRectMake(0, 0, self.CollectionBgScrollView.width, self.CollectionBgScrollView.height);
    self.SolidColorCollectionView.frame = CGRectMake(self.CollectionBgScrollView.width, 0, self.CollectionBgScrollView.width, self.CollectionBgScrollView.height);
    self.AuthorityCollectionView.frame = CGRectMake(self.CollectionBgScrollView.width*2, 0, self.CollectionBgScrollView.width, self.CollectionBgScrollView.height);
    
    
    
    if(self.topButtonTitleMarray.count<=0){
        
        
        //collectionTyp 20240919 我的  20240920 纯色
        //Id 20240919 我的  20240920 纯色
        xsBackgroundViewTopTitleModel *myModel = [xsBackgroundViewTopTitleModel new];
        myModel.name = @"我的";
        myModel.Id = 20240919;
        [self.topButtonTitleMarray addObject:myModel];
        
        xsBackgroundViewTopTitleModel *chunseModel = [xsBackgroundViewTopTitleModel new];
        chunseModel.name = @"纯色";
        chunseModel.Id = 20240920;
        [self.topButtonTitleMarray addObject:chunseModel];
        
        [self getTopTitleListData];
    }
    
    self.xsAuthorityBackGroundStyleBgView.frame = CGRectMake(0, 0, self.width, self.height);
    self.xsAuthorityBackGroundStyleTopRightCancelButton.frame = CGRectMake(self.xsAuthorityBackGroundStyleBgView.width-50, 0, 40, 40);
    self.xsAuthorityBackGroundStyleTopTitleLabel.frame = CGRectMake(50, 0, self.xsAuthorityBackGroundStyleBgView.width-100, 40);
    self.xsAuthorityBackGroundStyleBottomCheckButton.frame = CGRectMake(10, self.xsAuthorityBackGroundStyleBgView.height-50, self.xsAuthorityBackGroundStyleBgView.width-20, 40);
    self.xsAuthorityBackGroundStyleBottomCheckButton.layer.cornerRadius = 20.1;
    self.xsAuthorityBackGroundStyleBottomCheckButton.layer.masksToBounds = YES;
    
    
    CGFloat xsAuthorityBackGroundStyleCenterColloectionBgViewW = self.xsAuthorityBackGroundStyleBgView.width;
    CGFloat xsAuthorityBackGroundStyleCenterColloectionBgViewX = 0;
    CGFloat xsAuthorityBackGroundStyleCenterColloectionBgViewY = CGRectGetMaxY(self.xsAuthorityBackGroundStyleTopTitleLabel.frame);
    CGFloat xsAuthorityBackGroundStyleCenterColloectionBgViewH = CGRectGetMinY(self.xsAuthorityBackGroundStyleBottomCheckButton.frame)-xsAuthorityBackGroundStyleCenterColloectionBgViewY-5.0;
    self.xsAuthorityBackGroundStyleCenterColloectionBgView.frame = CGRectMake(xsAuthorityBackGroundStyleCenterColloectionBgViewX, xsAuthorityBackGroundStyleCenterColloectionBgViewY, xsAuthorityBackGroundStyleCenterColloectionBgViewW, xsAuthorityBackGroundStyleCenterColloectionBgViewH);
//    self.xsAuthorityBackGroundStyleBgView.hidden = NO;
//    self.xsAuthorityBackGroundStyleBgView.backgroundColor = UIColor.redColor;
    
    
    if(!_xsAuthorityBackGroundStyleCollectionV){
        
//// 使用系统自带的流布局（继承自UICollectionViewLayout）
//TWLayout *layout = ({
//    TWLayout *layout = [[TWLayout alloc] init];
//    CGFloat itemWithF = self.xsAuthorityBackGroundStyleCenterColloectionBgView.width-100.0;
//    CGFloat itemHeightF = self.xsAuthorityBackGroundStyleCenterColloectionBgView.height-60.0;
//    layout.itemSize                    = CGSizeMake(itemWithF, itemHeightF);
//    layout.scrollDirection             = UICollectionViewScrollDirectionHorizontal;
//    layout.minimumLineSpacing          = 10;//40;
//
//
//    //第一个cell和最后一个cell居中显示
//    CGFloat margin = (self.xsAuthorityBackGroundStyleCenterColloectionBgView.width - (itemWithF)) * 0.5;
//    layout.sectionInset  = UIEdgeInsetsMake(0, margin, 0, margin);
//
//    layout;
//});
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        layout.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        layout.minimumLineSpacing = 5.0;
        layout.scrollDirection             = UICollectionViewScrollDirectionHorizontal;
        
        
        // 使用UICollectionView必须设置UICollectionViewLayout属性
        xsAuthorityBackGroundStyleCollectionView *collectionView = ({
            
//            NSMutableArray *tempMarr = [[NSMutableArray alloc] init];
//            for (int i=0; i<6; i++) {
//                PhotoCellModel *tempMdd =  [PhotoCellModel new];
//                tempMdd.title = [NSString stringWithFormat:@"高清图%d",i+1];
//                tempMdd.url = @"https://hao123-static.cdn.bcebos.com/cms/2024-9/1725937009888/433c1fb71407.png";
//                [tempMarr addObject:tempMdd];
//            }
            
        //
            
            CGFloat collectionViewW = self.xsAuthorityBackGroundStyleCenterColloectionBgView.width;
            CGFloat collectionViewH = self.xsAuthorityBackGroundStyleCenterColloectionBgView.height;
            xsAuthorityBackGroundStyleCollectionView *collectionView = [[xsAuthorityBackGroundStyleCollectionView alloc] initWithFrame:CGRectMake(0, 0, collectionViewW, collectionViewH) collectionViewLayout:layout withDataArray:@[]];
//            collectionView.center            = self.xsAuthorityBackGroundStyleCenterColloectionBgView.center;
            //collectionView.bounds            = CGRectMake(0, 0, self.xsAuthorityBackGroundStyleCenterColloectionBgView.width, self.xsAuthorityBackGroundStyleCenterColloectionBgView.height);
            //collectionView.backgroundColor   = [UIColor brownColor];
            [collectionView setShowsHorizontalScrollIndicator:NO];
            
            [self.xsAuthorityBackGroundStyleCenterColloectionBgView addSubview:collectionView];
            
            collectionView;
        });
        self.xsAuthorityBackGroundStyleCollectionV = collectionView;
        collectionView.AuthorityBackGroundDelegate = self;
        //collectionView.backgroundColor = UIColor.redColor;
    }
}


#pragma mark  ScollViewDelegate
// 滚动视图减速完成，滚动将停止时，调用该方法。一次有效滑动，只执行一次。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"scrollViewDidEndDecelerating");
    if(scrollView==self.CollectionBgScrollView){
        int pageIndex = scrollView.contentOffset.x/self.CollectionBgScrollView.width;
        
        NSLog(@"scrollViewDidEndDecelerating--pageIndex==%d\n",pageIndex);
        for (UIButton *tempButton in self.buttonsBgView.subviews) {
            if([tempButton isKindOfClass:[UIButton class]])
            {
                if(pageIndex==tempButton.tag){
                    [self topTitleButtonBeClicked:tempButton];
                    break;
                }
            }
        }
    }
}
- (void)updateConstraints{
    [super updateConstraints];
//    
//    __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");
    
}

- (xsBossMyCollectionView *)BossMyCollectionView{
    
    if(!_BossMyCollectionView){
        
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        
//        NSMutableArray *tempMarr = [NSMutableArray new];
//        for (int i =0; i<26; i++) {
//            xsBossMyCollectionViewCellModel *md = [xsBossMyCollectionViewCellModel new];
//            md.url = @"bossIPDefaultImage";
//            if(i!=0){
//                md.isAddType = NO;
//            }
//            else{
//                md.isAddType = YES;
//            }
//            [tempMarr addObject:md];
//        }
        
        XSWeakSelf;
        _BossMyCollectionView = [[xsBossMyCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withAspectRatio:1 withType:100];
        _BossMyCollectionView.backgroundColor = UIColor.blackColor;
        _BossMyCollectionView.BossMyCollectionCellCallBackBlock = ^(id model,NSInteger collectionTyp) {
            XSLogMethod2(model)
           
            if([weakSelf.delegate respondsToSelector:@selector(xsBackgroundCollectionViewCellCallBackWithModel:withCollectionTyp:)]){
                [weakSelf.delegate xsBackgroundCollectionViewCellCallBackWithModel:model withCollectionTyp:collectionTyp];
            }
        };
        
        _BossMyCollectionView.BossMyCollectionCellDeleteButtonCallBackBlock = ^(id  _Nonnull model, NSInteger collectionTyp) {
            
            if([weakSelf.delegate respondsToSelector:@selector(xsBackgroundCollectionViewMyCollectionCellDeleteButtonCallWithModel:)]){
                [weakSelf.delegate xsBackgroundCollectionViewMyCollectionCellDeleteButtonCallWithModel:model];
            }
        };
        
    }
    return _BossMyCollectionView;
}
- (xsSolidColorCollectionView *)SolidColorCollectionView{
    
    if(!_SolidColorCollectionView){
        
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        
        NSArray *tempColorArr =  [UIColor get84Color];
        
//        @[
//            @"#ff0000",
//            @"#ff7f00",
//            @"#ffff00",
//            @"#00ff00",
//            @"#0000ff",
//            @"#4b0082",
//            @"#9400d3",
//            @"#ff1493",
//            @"#ff69b4",
//            @"#ffc0cb",
//            @"#ffa500",
//            @"#ffd700",
//            @"#ffffff",
//            @"#000000",
//          ];
        NSMutableArray *tempMarr = [NSMutableArray new];
        for (int i =0; i<tempColorArr.count; i++) {
            xsSolidColorCollectionViewCellModel *md = [xsSolidColorCollectionViewCellModel new];
            md.iconUrlString = @"bossIPDefaultImage";
            md.bossBgColor = tempColorArr[i];
            [tempMarr addObject:md];
        }
        
        XSWeakSelf;
        _SolidColorCollectionView = [[xsSolidColorCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withData:tempMarr];
        _SolidColorCollectionView.backgroundColor = UIColor.blackColor;
        
        _SolidColorCollectionView.SolidColorCollectionCellCallBackBlock = ^(id model,NSInteger collectionTyp) {
            XSLogMethod2(model)
           
            if([weakSelf.delegate respondsToSelector:@selector(xsBackgroundCollectionViewCellCallBackWithModel:withCollectionTyp:)]){
                [weakSelf.delegate xsBackgroundCollectionViewCellCallBackWithModel:model withCollectionTyp:collectionTyp];
            }
        };
        
    }
    return _SolidColorCollectionView;
}


- (UIView *)topView{
    
    if(!_topView){
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = UIColor.clearColor;
    }
    return _topView;
}



- (UIButton *)xsXbutton{
    if (!_xsXbutton) {
        _xsXbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsXbutton.backgroundColor = UIColor.clearColor;
        [_xsXbutton addTarget:self action:@selector(xsXbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsXbutton setImage:[UIImage imageNamed:@"xsBossgou"] forState:UIControlStateNormal];
    }
    return _xsXbutton;
}
- (void)xsXbuttonAction:(UIButton *)btn{
    NSLog(@"xsXbuttonAction\n\n");
   
    if([self.delegate respondsToSelector:@selector(xsBackgroundViewXbuttonBeClick:)]){
        [self.delegate xsBackgroundViewXbuttonBeClick:btn];
    }
}

- (UIButton *)searchButton{
    if (!_searchButton) {
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchButton.backgroundColor = UIColor.purpleColor;
        [_searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_searchButton setImage:[UIImage imageNamed:@"xsBossIPX"] forState:UIControlStateNormal];
        _searchButton.hidden = YES;
    }
    return _searchButton;
}
- (void)searchButtonAction:(UIButton *)btn{
    NSLog(@"searchButtonAction\n\n");
   
    
}
- (UILabel *)topTitleLabel {
    if (!_topTitleLabel) {
        _topTitleLabel = [[UILabel alloc] init];
        _topTitleLabel.backgroundColor = UIColor.clearColor;
        _topTitleLabel.font = [UIFont systemFontOfSize:21];// FontSemibold(17);
        _topTitleLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _topTitleLabel.numberOfLines = 1;
        _topTitleLabel.text = @"选择背景";
        _topTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _topTitleLabel;
}


#pragma mark 顶部标题列表
- (void)getTopTitleListData{
    //
        
    XSWeakSelf;
    NSString *tempUrl = [NSString stringWithFormat:@"%@%@",API_BASEURL_BossIP,@"backgroundImage/category"];
    NSDictionary *pramerDict = @{};
    [[ZYFDataManager sharedInstance] GET:tempUrl dict:pramerDict succeed:^(id data) {
        
        NSLog(@"getTopTitleListData--tempUrl==%@---pramerDict==%@--data==%@\n",tempUrl,pramerDict,data);
        
        NSDictionary *dictData = data;
        if([dictData isKindOfClass:[NSDictionary class]]){
            if([dictData[@"code"] isKindOfClass:[NSNumber class]]||[dictData[@"code"] isKindOfClass:[NSString class]]){
                NSInteger codeInt = [dictData[@"code"] integerValue];
                if(codeInt==200){
                    if([dictData[@"data"] isKindOfClass:[NSArray class]]){
                        NSArray *dataArr3  = dictData[@"data"];

                        
                        if(dataArr3.count>0){
                            
                            for (int ii=0; ii<dataArr3.count; ii++) {
                                NSDictionary *tempFinalDict = dataArr3[ii];
                                xsBackgroundViewTopTitleModel *md = [xsBackgroundViewTopTitleModel yy_modelWithDictionary:tempFinalDict];
                                    if([tempFinalDict[@"id"] isKindOfClass:[NSNumber class]]||
                                       [tempFinalDict[@"id"] isKindOfClass:[NSString class]]){
                                       NSString *tempIdStr = [NSString stringWithFormat:@"%ld",[tempFinalDict[@"id"] integerValue]];
                                        md.Id = [tempIdStr integerValue];
                                    }
                                [weakSelf.topButtonTitleMarray addObject:md];
                            }

                            dispatch_async(dispatch_get_main_queue(), ^{
                                [weakSelf createTopButtons];
                            });
                        }
                    }
                }
            }
        }
        
        
    } failure:^(NSDictionary *error) {
        NSLog(@"getTopTitleListData--error.description==%@\n",error.description);
    }];
}


- (void)createTopButtons{
    
    
    UIFont *btnFont = Font(16);


    UIButton *lastButton;
    // 添加按钮到滚动视图
    for (int i = 0; i < self.topButtonTitleMarray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        xsBackgroundViewTopTitleModel *tpMd = self.topButtonTitleMarray[i];
        NSString *buttonTitleStr = tpMd.name;

        CGFloat buttonW = [buttonTitleStr calculateWidth:btnFont withHeight:40]+25.0;
    //        button.frame = CGRectMake(i * buttonW, 0, buttonW, 40);
        button.backgroundColor = UIColor.clearColor;//xrandomColor;
        [button setTitle:buttonTitleStr forState:UIControlStateNormal];
        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(topTitleButtonBeClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [self.buttonsBgView addSubview:button];
        if(lastButton){
            button.frame = CGRectMake(CGRectGetMaxX(lastButton.frame), 0, buttonW, 40);
            button.font = btnFont;
        }
        else{
            button.frame = CGRectMake(0, 0, buttonW, 40);
            button.font = FontBoldMT(16);
        }
        lastButton = button;
    }

    // 设置内容视图的尺寸
    self.buttonsBgView.frame = CGRectMake(0, 0, CGRectGetMaxX(lastButton.frame), 40);
    self.buttonsBgScrollView.contentSize = self.buttonsBgView.frame.size;
}


- (void)topTitleButtonBeClicked:(UIButton *)sender {
    NSInteger selectedIndex = sender.tag;
    NSLog(@"Selected index: %ld", (long)selectedIndex);
    
//    // 在这里切换底部视图
//    if(selectedIndex==0){// 我的
//
//    }
//    else if(selectedIndex==1){//纯色
//
//    }
//    else if(selectedIndex==2){//官方
//
//    }
    for (UIButton *tempButton in self.buttonsBgView.subviews) {
        if([tempButton  isKindOfClass:[UIButton class]]){
            if(tempButton.tag!=sender.tag){
                tempButton.font = Font(16);
            }
            else{
                tempButton.font = FontBoldMT(16);
            }
        }
    }
    
    XSWeakSelf;
    if(sender.tag == 0){
        // 20240919我的  20240920纯色
        
        [UIView animateWithDuration:0.3 animations:^{
            [weakSelf.CollectionBgScrollView setContentOffset:CGPointMake(0, 0)];
        }];
    }
    else if(sender.tag == 1){
        // 20240919我的  20240920纯色
        
        [UIView animateWithDuration:0.3 animations:^{
            [weakSelf.CollectionBgScrollView setContentOffset:CGPointMake(weakSelf.CollectionBgScrollView.width, 0)];
        }];
    }
    else{
        [UIView animateWithDuration:0.3 animations:^{
            [weakSelf.CollectionBgScrollView setContentOffset:CGPointMake(weakSelf.CollectionBgScrollView.width*2, 0)];
        }];
        xsBackgroundViewTopTitleModel *tpMd = self.topButtonTitleMarray[selectedIndex];
        [self.AuthorityCollectionView xsAuthorityCollectionLoadDataWithId:tpMd.Id];
    }
    
    
    
}


- (xsAuthorityCollectionView *)AuthorityCollectionView{
    
    if(!_AuthorityCollectionView){
        
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        
//        NSMutableArray *tempMarr = [NSMutableArray new];
//        for (int i =0; i<26; i++) {
//            xsAuthorityCollectionViewCellModel *md = [xsAuthorityCollectionViewCellModel new];
//            md.iconUrlString = @"bossIPDefaultImage";
//            [tempMarr addObject:md];
//        }
//
//
        XSWeakSelf;
        _AuthorityCollectionView = [[xsAuthorityCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withAspectRatio:0];
        _AuthorityCollectionView.backgroundColor = UIColor.blackColor;
        _AuthorityCollectionView.AuthorityCollectionCellCallBackBlock = ^(id model,NSInteger collectionTyp) {
            
           XSLogMethod2(model)
//            if([weakSelf.delegate respondsToSelector:@selector(xsBackgroundCollectionViewCellCallBackWithModel:withCollectionTyp:)]){
//                [weakSelf.delegate xsBackgroundCollectionViewCellCallBackWithModel:model withCollectionTyp:collectionTyp];
//            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf xsShowAuthorityBackGroundStyleCollectionViewWithModel:model];
            });
            
        };
    }
    return _AuthorityCollectionView;
}


- (void)xsShowAuthorityBackGroundStyleCollectionViewWithModel:(xsAuthorityCollectionViewCellModel *)model{
    
    
//    self.xsAuthorityBackGroundStyleCollectionV = collectionView;
    
    if(self.xsAuthorityBackGroundStyleCollectionV){
        NSMutableArray *tempMarr = [[NSMutableArray alloc] init];
        
        //url高清图
        //blurUrl    全屏虚化图片地址
        //gradientBlurUrl    四周虚化图片地址
        //dynamicUrl    动图地址
        
        if(!kStringIsEmpty(model.url)){
            PhotoCellModel *tempMdd =  [PhotoCellModel new];
            tempMdd.title = @"高清";
            tempMdd.url = model.url;
            tempMdd.Id = model.Id;
            [tempMarr addObject:tempMdd];
        }
        
        if(!kStringIsEmpty(model.blurUrl)){
            PhotoCellModel *tempMdd =  [PhotoCellModel new];
            tempMdd.title = @"全屏虚化";
            tempMdd.url = model.blurUrl;
            tempMdd.Id = model.Id;
            [tempMarr addObject:tempMdd];
        }
        
        
        
        if(!kStringIsEmpty(model.gradientBlurUrl)){
            PhotoCellModel *tempMdd2 =  [PhotoCellModel new];
            tempMdd2.title = @"四周虚化";
            tempMdd2.url = model.gradientBlurUrl;
            tempMdd2.Id = model.Id;
            [tempMarr addObject:tempMdd2];
        }
        
        
        
        if(!kStringIsEmpty(model.dynamicUrl)){
            PhotoCellModel *tempMdd3 =  [PhotoCellModel new];
            tempMdd3.title = @"动图";
            tempMdd3.url = model.dynamicUrl;
            tempMdd3.Id = model.Id;
            [tempMarr addObject:tempMdd3];
        }
        
        if(tempMarr.count>0){
            self.xsAuthorityBackGroundStyleBgView.hidden = NO;
            [self.xsAuthorityBackGroundStyleCollectionV xsUpdateCollectionViewData:tempMarr];
        }
        else{
            [MBProgressHUD showError:kLocalizedString(@"暂无图片选择!")];
        }
    }
    
}
- (void)xsAuthorityBackGroundStyleCellCallBackWithModel:(PhotoCellModel *)cellModel{
    
//    if(_AuthorityCollectionView){
//        NSArray *dArr = [_AuthorityCollectionView xsCollectionViewDataArray];
//        if(dArr.count>0){
//            xsAuthorityCollectionViewCellModel *findMd;
//            for (xsAuthorityCollectionViewCellModel *tempMd in dArr) {
//                tempMd.
//            }
//        }
//    }
    
    self.recodeSelectPhotoCellMd = cellModel;
}

// 我的collecitonview重新获取数据
- (void)xsBossBackgroundVTellMyCollectionViewReloadData{
    
    [_BossMyCollectionView xsBossBackgroundVTellMyCollectionViewReloadDataFunction];
}
@end
